﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

using ParamSetCollection.General.Parameters.Normal;

namespace ParamSetCollection.Modules.CurveCtrl
{
    class ChannelKeeper
    {

        #region============================== 变量 ====================================

        string pathRoot=System.Environment.CurrentDirectory + "\\Curve\\";
        string path = "";

        TimeSpan m_interval;//频率，预留
        ParameterUnit m_pu = null;
        DateTime m_renewRefDate = DateTime.Now;
        DateTime m_saveRefDate = DateTime.Now;

        Thread thr = null;
        bool m_start = false;

        Dictionary<int, Dictionary<int, ChannLogDataUnit>> dataDic = new Dictionary<int, Dictionary<int, ChannLogDataUnit>>();
        #endregion=====================================================================


        #region============================== 属性 ====================================
        public string Code { get { return m_pu.Code; } }

        #endregion=====================================================================


        #region============================= 初始化函数 ================================
        private void InitDatas()
        {
            dataDic.Clear();

            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 60; j++)
                {
                    ChannLogDataUnit cu = new ChannLogDataUnit(i,j);

                }
            }

            string dir=pathRoot + m_pu.Code + m_renewRefDate.ToString("\\yyyy\\MM\\");
            if(Directory.Exists(dir)==false)
                Directory.CreateDirectory(dir);

            path = dir + m_renewRefDate.ToString("dd\\") + ".txt";

            if (File.Exists(path) == false)
            {
                File.Create(path);
                Save(path);
            }
            else
            {
                Read(path);
            }
        }


        #endregion====================================================================


        #region============================= 功能函数 ================================

        private void Save(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path);


                for (int i = 0; i < 24; i++)
                {
                    for (int j = 0; j < 60; j++)
                    {
                        string str = "";
                        str = dataDic[i][j].TimeLabel + " ";
                        for (int k = 0; k < 60; k++)
                        {
                            str = str + dataDic[i][j].DataList[k];
                            if (k < 59)
                                str = str + ",";
                        }
                        sw.WriteLine(str);
                    }
                }

                sw.Close();
            }
            catch (Exception ex)
            {



            }





        }

        private void Read(string path)
        {
            if(File.Exists(path)==true)
            {
                StreamReader sr = new StreamReader(path);
                while (sr.EndOfStream == false)
                {
                    string[] str=sr.ReadLine().Split(' ');
                    string[] str2 = str[0].Split(':');

                    int a, b;
                    if(int.TryParse(str2[0], out a) && int.TryParse(str2[1], out b))
                    {
                        dataDic[a][b].InitValue(str[1]);
                    }
                 ;
                }

            }

        }

        private void ThreadProcess()
        {
            while (m_start)
            {
                DateTime dt = DateTime.Now;
                TimeSpan ts = dt - m_renewRefDate;

                string dir = pathRoot + m_pu.Code + m_renewRefDate.ToString("\\yyyy\\MM\\");
                string pathBuff = dir + m_renewRefDate.ToString("dd\\") + ".txt";


                //定时更新值
                if (path == pathBuff && ts.TotalSeconds >= 1)
                {
                    dataDic[dt.Hour][dt.Minute].DataList[dt.Second] = m_pu.ViewValue;
                }

                //定时保存
                ts = dt - m_saveRefDate;
                if (ts.TotalSeconds >= 60)
                {
                    Save(path);
                    m_saveRefDate = dt;
                }


                //判断日期变更
                if(path!=pathBuff)
                {
                    m_renewRefDate = dt;
                    InitDatas();

                }

                Thread.Sleep(300);
            }

        }
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public ChannelKeeper(ParameterUnit pu,TimeSpan ts)
        {
            m_pu = pu;

            m_interval = ts;

            InitDatas();
        }

        public void Start()
        {
            m_renewRefDate = DateTime.Now;
            m_start = true;
            thr = new Thread(new ThreadStart(ThreadProcess));
            thr.IsBackground = true;
            thr.Start();
        }

        public void Stop()
        {
            m_start = false;


        }

        public void GetCurveData(DateTime dt)
        {

        }

        #endregion====================================================================


        #region============================= 消息响应 ================================
        #endregion====================================================================

    }
}
